<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Http\Models\Contact;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class ContactController extends Controller
{
    /**
     * 获取客户、供货商列表
     * @param Request $request
     * @author : zl
     * @email : 932460566@qq.com
     * @date : 2020-07-18
     */
    public function get_list(Request $request){
        $data = $request->all();
        $sql = ' isDelete = ? AND type = ? ';
        $field = [0,10];
        $data['type'] = isset($data['type']) && $data['type'] == 10 ? 10 : -10;
        $field[1] = $data['type'];
        if(isset($data['keyword']) && $data['keyword']) {
            $access = ['number', 'name', 'linkMans'];
            $sql .= ' AND (';
            foreach ($access as $v) {
                $sql .=   $v . ' like ' . ' ?  OR ';
                $field[] = '%'.$data['keyword'].'%';
            }
            $sql = rtrim($sql,'  OR ');
            $sql .= ' )';
        }
//        dd($sql,$field);
        $pageSize = $data['pageSize'] ?? 20;
        //开启数据库日志查询
//        DB::enableQueryLog();
        $list = Contact::select(['*','name as title'])->whereRaw($sql,$field)->paginate($pageSize)->toArray();
//        dd($list);
        $returnList = [];
        foreach ($list['data'] as $arr=>$row) {
            $returnList[$arr]['id']           = intval($row['id']);
            $returnList[$arr]['number']       = $row['number'];
            $returnList[$arr]['cCategory']    = intval($row['cCategory']);
            $returnList[$arr]['customerType'] = $row['cCategoryName'];
            $returnList[$arr]['pinYin']       = $row['pinYin'];
            $returnList[$arr]['name']         = $row['name'];
            $returnList[$arr]['type']         = $row['type'];
            $returnList[$arr]['delete']       = intval($row['disable'])==1 ? true : false;
            $returnList[$arr]['cLevel']       = intval($row['cLevel']);
            $returnList[$arr]['amount']       = (float)$row['amount'];
            $returnList[$arr]['periodMoney']  = (float)$row['periodMoney'];
            $returnList[$arr]['difMoney']     = (float)$row['difMoney'];
            $returnList[$arr]['remark']       = $row['remark'];
            $returnList[$arr]['taxRate']      = (float)$row['taxRate'];
            $returnList[$arr]['links']        = '';
            $returnList[$arr]['linkMen']      = $row['linkMans'];
            if (strlen($row['linkMans'])>0) {
                $list = (array)json_decode($row['linkMans'],true);
                foreach ($list as $arr1=>$row1) {
                    if ($row1['linkFirst']==1) {
                        $returnList[$arr]['contacter']            = isset($row1['linkName']) && $row1['linkName'] ? $row1['linkName'] : '' ;
                        $returnList[$arr]['mobile']               = isset($row1['linkMobile']) && $row1['linkMobile'] ? $row1['linkMobile'] : '';
                        $returnList[$arr]['place']                = isset($row1['linkPlace']) && $row1['linkMobile'] ? $row1['linkPlace'] : '';
                        $returnList[$arr]['telephone']            = isset($row1['linkPhone']) && $row1['linkPhone'] ? $row1['linkPhone'] : '';
                        $returnList[$arr]['linkIm']               = isset($row1['linkIm']) && $row1['linkIm'] ? $row1['linkIm'] : '';
                        $returnList[$arr]['city']                 = isset($row1['city']) && $row1['city'] ? $row1['city'] : '';
                        $returnList[$arr]['county']               = isset($row1['county']) && $row1['county'] ? $row1['county'] : '';
                        $returnList[$arr]['province']             = isset($row1['province']) && $row1['province'] ?  $row1['province'] : '';
                        $returnList[$arr]['deliveryAddress']      = isset($row1['address']) && $row1['address'] ? $row1['address'] : '';
                        $returnList[$arr]['firstLink']['first']   = isset($row1['linkFirst']) && $row1['linkFirst'] ? $row1['linkFirst'] : '';
                    }
                }
            }
        }
        $list['data'] = $returnList;
//        打印最后的sql语句
//        dd(DB::getQueryLog());
        $this->show_msg(['status'=>'success','msg'=>'数据获取成功！','data'=>$list,'success'=>true]);
    }
}
